<?php

use yii\helpers\Html;
use yii\widgets\LinkPager;
use yii\grid\GridView;
use yii\bootstrap\Modal;
use yii\helpers\Url;
use app\common\Functions;

/**
 * @var yii\web\View $this
 * @var amnah\yii2\user\Module $module
 * @var array $actions
 */
global $regionTypes2;
global $prodAttrCodes;
$regionTypes2 = $regionTypes;
$module = $this->context->module;

$this->title = Yii::t('user', 'Yii 2 User');
?>


<p>
    <?php
    $createUrl                     = [];
    $createUrl[]                   = 'createmodal';
    $createUrl['attr_type_spec_id']   = $attr_type_spec_id;
    $createUrl['prod_spec_name'] = $prodSpecName;


//    //创建区域逻辑
//    // 如果创建
//    echo Html::a( Html::tag('span', Yii::t('region', 'Create') .  $prodSpecName ,
//            ['class' => "glyphicon fa fa-plus-square"])   . $region_name. Yii::t('region', $region_type_name) ,
//      "#",
//        ['class' => 'btn btn-success activity-cust-create-link','data-toggle'=>'modal','data-target'=>'#cust-create-modal','data-attr_type_spec_id'=>$attr_type_spec_id,'data-prod_spec_name'=>$prodSpecName]);


//    echo Html::a('创建' .  $prodSpecName , '#', [
//        'id' => 'create',
//        'data-toggle' => 'modal',
//        'data-target' => '#create-modal',
//        'class' => 'btn btn-success',
//    ]);
    ?>

</p>

<?php
$prodAttrCodes=[];

// Yii::trace("=====================              ============");
//Yii::trace($dataProvider);


// 增加展示的字段
$columns = [];
$i = 0;

// 复选框
$columns[$i]['class'] = 'yii\grid\CheckboxColumn';
$columns[$i]['multiple'] = true;
$columns[$i]['name'] = 'uid';
$columns[$i]['options'] = ['style'=>'width:15px'];

//$i++;
//// id
//$columns[$i]['attribute'] = 'id';
//$columns[$i]['options'] = ['style'=>'width:30px'];
$i++;

$i='early_time';
//更新时间
$columns[$i]['label'] = '来访天数';
$columns[$i]['attribute'] = 'early_time';
$columns[$i]['options'] = ['style'=>'width:30px'];
$columns[$i]['content']   = function($model)
{
    $date= time();
    $sum=strtotime($model->create_date)  ;
    $sun=$date-$sum;
    return   round(($sun)/3600/24)  . '天' ;

};

$i++;


$columns[$i]['label'] = '初次来访时间';
$columns[$i]['attribute'] = 'create_date';
$columns[$i]['options'] = ['style'=>'width:60px'];
$columns[$i]['content']   = function($model)
{
    return  substr($model->create_date,0,10)   ;
};


$i++;
//名称
$columns[$i]['label'] = '客户名称';
$columns[$i]['attribute'] = 'cust_name';
$columns[$i]['options'] = ['style'=>'width:40px'];
$columns[$i]['filter'] = Html::activeTextInput($searchModel, 'cust_name', ['class'=>'form-control']);









foreach ($attrSpecs as $attrSpec)
{
    $prodAttr = $attrSpec->attrSpecs;

    // 如果是不显示的属性这里要跳过
    if ($attrSpec->grid_show_flag!=1)
    {
        continue;
    }

    if($prodAttr->attr_spec_name=='性别' || $prodAttr->attr_spec_name=='联系人'){

    }else{
        $i++;
        $columns[$i]['label'] = $prodAttr->attr_spec_name;
        $prodAttrCodes[$i] = $prodAttr->attr_spec_id;
        $a = $prodAttr->attr_spec_id;



        $attr_spec_type_id = $prodAttr->attr_spec_type_id;

        $columns[$i]['value']   = function($model, $key, $index, $column)use ($a, $attr_spec_type_id)
        {
            if ($model->custExternalAttrs)
            {
                foreach ($model->custExternalAttrs  as $attrInst)
                {
                    if ($a == $attrInst->attr_spec_id)
                    {
                        if ($attr_spec_type_id==3)
                        {
                            return Functions::getDomainsName('attr_spec',$a,  $attrInst->content);
                        }
                        else if($attr_spec_type_id==4)
                        {
                            return $attrInst->content . '-'.$attrInst->content2 ;
                        }

                        return $attrInst->content;

                    }
                    if ($a == $attrInst->attr_spec_id)
                    {
                        return $attrInst->content;
                    }
                }
            }

            return  '';

        };
    }




    $columns[$i]['options'] = ['style'=>'width:60px'];

}

$i++;
//名称
$columns[$i]['label'] = '全部途径';
$columns[$i]['attribute'] = 'cust_source_id';
$columns[$i]['options'] = ['style'=>'width:45px'];
$columns[$i]['value']   = function($model) use ($domainSources)
{
    return $domainSources[$model->cust_source_id];



};

$i++;
//名称
$columns[$i]['label'] = '客户意向';
$columns[$i]['attribute'] = 'cust_type_id';
$columns[$i]['options'] = ['style'=>'width:45px'];
$columns[$i]['value']   = function($model) use ($domainTypes)
{
    return $domainTypes[$model->cust_type_id];

};

        // 自定义样式
         //echo Html::dropDownList('username', null, ArrayHelper::map($regionTypes,'id', 'region_type_name'), ['class' => 'dropdownlist']);




// 最大最小面积特殊显示先， 因为是两个值要展示在一个框里
//$i++;
////名称
//$columns[$i]['label'] = '需求面积';
////$columns[$i]['attribute'] = 'cust_name';
//$columns[$i]['options'] = ['style'=>'width:180px'];
//$columns[$i]['value']   = function($model, $key, $index, $column){
//
//    $minArea = 0;
//    $maxArea = 0;
//    foreach ($model->custExternalAttrs  as $attrInst)
//    {
//        if ($attrInst->attr_spec_id==11)
//        {
//            $minArea = $attrInst->content;
//        }
//
//        if ($attrInst->attr_spec_id==12)
//        {
//            $maxArea = $attrInst->content;
//        }
//    }
//
//    return  $minArea  . '-'.  $maxArea . '㎡';
//
//};

$i++;

$columns[$i]['label'] = '跟进信息';
$columns[$i]['attribute'] = 'prod_message';
$columns[$i]['options'] = ['style'=>'width:150px'];

$columns[$i]['content']   = function($model)
{
    //return  $model->remark ;//要倒序(单独查) order by a.create_date desc
    $txt=$model->getOrderResumesText($model->id);
    return  $txt;
};


$i++;


$columns[$i]['label'] = '观看房源';
$columns[$i]['attribute'] = 'prod_sum';
$columns[$i]['options'] = ['style'=>'width:30px'];

$columns[$i]['content']   = function($model)
{
    $sum=$model->getUseCustCount($model->id);
    if (!$sum)
    {
        $sum=0;
    }

   return Html::a($sum,
        '#' ,
        ['id' => 'activity-cancel-sum-link',
            'class' => "activity-cancel-sum-link",
            'data-toggle'=>'modal',
            'data-target'=>'#cancel-sum-modal',
            'title' => '数量',
            'data-order-id'=>$model->id]);
};


$i++;
//名称
$columns[$i]['label'] = '创建人';
$columns[$i]['attribute'] = 'create_staff_id';
$columns[$i]['options'] = ['style'=>'width:40px'];
$columns[$i]['filter'] = Html::activeTextInput($searchModel, 'user_name', ['class'=>'form-control']);
$columns[$i]['content']   = function($model)
{
    return $model->users->username  ;
};






$i++;
//名称
$columns[$i]['label'] = '跟进时间';
$columns[$i]['attribute'] = 'modify_date';
$columns[$i]['options'] = ['style'=>'width:60px'];
$columns[$i]['content']   = function($model)
{
     return  substr($model->modify_date,0,10)   ;
};





$i++;
//操作选项
//$columns[$i]['class']   = 'app\common\grid\CustActionColumn';
$columns[$i]['options'] = ['style'=>'width:50px'];

$columns[$i]['content']   = function($model) use ($prodSpecName, $now_url, $attr_type_spec_id)
{


    $btns = [];

//    $btnBegin = Html::a( '修改', '#',
//        ['id' => 'activity-cust-update-link',
//            'class' => "btn btn-xs btn-warning activity-cust-update-link",
//            'data-toggle'=>'modal',
//            'data-target'=>'#cust-update-modal',
//
//            'data-now_url'  => $now_url ,
//            'data-attr_type_spec_id'=>$attr_type_spec_id,
//            'data-prod_spec_name'=>$prodSpecName,
//
//            'label'=>'更新客户信息', 'title' => '更新客户信息',
//            'data-cust-id'=>$model->id]);
//
//    $btns[]   = Html::a( '修改', '#', ['id' => 'activity-cust-update-link',
//        'class' => "activity-cust-update-link",
//        'data-toggle'=>'modal',
//        'data-target'=>'#handling-resumes-modal',
//        'label'=>'更新客户信息', 'title' => '更新客户信息',
//        'data-cust-id'=>$model->id]);
//
//
//    $btns[]   = Html::a( '查看',['view', 'id'=>$model->id,   'attr_type_spec_id'=> $model->cust_type_id,    'prod_spec_name' => $prodSpecName ], [ 'label'=>'查看客户详情', 'title' => '查看客户详情']);
//    $btns[]   = Html::a( '删除',['delete', 'id'=>$model->id,   'delete', 'id'=>$model->id,   'attr_type_spec_id'=> $model->cust_type_id ], [ 'label'=>'删除客户', 'title' => '删除客户']);


    $btnBegin  = Html::a( '查看进度',['/prod/cust/view', 'id'=>$model->id,   'attr_type_spec_id'=> 20001 , 'order_id'=>$model->orders->id,'timeline'=>'timeline' ], ['class' => 'btn btn-xs btn-warning ', 'label'=>'跟进记录', 'title' => '跟进记录']);


    $btns[]   = Html::a( '进度更新', '#', ['id' => 'activity-handling-resumes-link',
        'class' => "activity-handling-resumes-link",
        'data-toggle'=>'modal',
        'data-target'=>'#handling-resumes-modal2',
        'label'=>'更新进度', 'title' => '更新进度',
        'data-order-id'=>$model->orders->id,
        'data-last_url' =>$now_url]);

    $btns[]   = Html::a( '需求更新',  ['/prod/cust/update',
        'id'=>$model->id,
        'order_id'=>$model->orders->id,
        'attr_type_spec_id'=> $model->orders->object_type_id,
        'prod_spec_name' => '订单',
        'last_url' =>$now_url],
        [ 'label'=>'修改', 'title' => '修改']);

    $btns[]   = Html::a( '变更负责人',   '#',
        ['id' => 'activity-modify-uesr-link',
            'class' => "activity-modify-uesr-link",
            'data-toggle'=>'modal',
            'data-target'=>'#modify-uesr-modal',
            'label'=>'变更负责人', 'title' => '变更负责人',
            'data-order-id'=>$model->orders->id,
            'data-last_url' =>$now_url]);

    $btns[]   = Html::a( '标记无效',   '#',
        ['id' => 'activity-cancel-order-link',
            'class' => "activity-cancel-order-link",
            'data-toggle'=>'modal',
            'data-target'=>'#cancel-order-modal',
            'label'=>'订单无效', 'title' => '订单无效',
            'data-order-id'=>$model->orders->id,
            'data-attr_type_spec_id'=> $model->orders->object_type_id ,
            'data-last_url' =>$now_url]);

    return Functions::getActionDropdownContext($btnBegin, $btns);

};



echo GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'layout' => "{items}{summary}{pager}",
    'options'=>['id'=>'grid', 'class'=>'col-sm-12'],
    'columns' => $columns

]);

$js = <<<JS
jQuery('.activity-cust-create-link').on('click',function() {
    $('.document-nav-cust-create-form').remove();
    $.get(
        '/prod/cust/createmodal',          
        {
            //attr_type_spec_id: $(this).data('attr_type_spec_id'),
            //prod_spec_name: $(this).data('prod_spec_name') ,
            // last_url: $(this).data('now_url')
        },
        function (data) {
            $('.modal-dialog').html(data);
            $('#cust-create-modal').modal();
        }  
    );
});
JS;
$this->registerJs($js);

$js = <<<JS
jQuery('.activity-cust-update-link').on('click',function() {
    $('.document-nav-cust-update-form').remove();
    $.get(
        '/prod/cust/updatemodal',          
        {
            //cust_id: $(this).data('cust-id'),
            //attr_type_spec_id: $(this).data('attr_type_spec_id'),
            //prod_spec_name: $(this).data('prod_spec_name') ,
            //last_url: $(this).data('now_url')
        },
        function (data) {
            $('.modal-dialog').html(data);
            $('#cust-create-modal').modal();
        }  
    );
});
JS;
$this->registerJs($js);


$js = <<<JS
jQuery('.activity-handling-resumes-link').on('click',function() {
    $('.document-handling-resumes-form').remove();
    $.get(
        '/prod/order/handling',
        {
            order_id: $(this).data('order-id') ,
            last_url: $(this).data('last_url')
        },
        function (data) {

            $('#handling-resumes-modal2').find('.modal-dialog').html(data);
        }
    );
});
JS;
$this->registerJs($js);//将一段js代码写入registerJs


$js = <<<JS

jQuery('.activity-modify-uesr-link').on('click',function() {
    $('.document-modify-uesr-form').remove();
    $.get(
        '/prod/order/modifyuser',
        {
            order_id: $(this).data('order-id') ,
            last_url: $(this).data('last_url')
        },
        function (data) {

            $('#modify-uesr-modal').find('.modal-dialog').html(data);

        }
    );
});
JS;
$this->registerJs($js);

//作废
$js = <<<JS
jQuery('.activity-cancel-order-link').on('click',function() {
    $('.document-cancel-order-form').remove();
    $.get(
        '/prod/order/cancel',
        {
            order_id: $(this).data('order-id') ,
            last_url: $(this).data('last_url') ,
            attr_type_spec_id: $(this).data('attr_type_spec_id')
        },
        function (data) {
            $('#cancel-order-modal').find('.modal-dialog').html(data);

        }
    );
});
JS;
$this->registerJs($js);


Modal::begin([
    'id' => 'handling-resumes-modal2',
    'class' => 'modal modal-primary',
    'header' => '<h4 class="modal-title">更新订单状态</h4>',
    'footer' => '<a href="#" class="btn btn-primary" data-dismiss="modal">Close</a>',
    'options' => [
        'tabindex' => false
    ],
]);
Modal::end();


Modal::begin([
    'id' => 'modify-uesr-modal',
    'class' => 'modal modal-primary',
    'header' => '<h4 class="modal-title">修改负责人</h4>',
    'footer' => '<a href="#" class="btn btn-primary" data-dismiss="modal">Close</a>',
    'options' => [
        'tabindex' => false
    ],
]);
Modal::end();


Modal::begin([
    'id' => 'cust-create-modal',
    'class' => 'modal modal-primary',
    'header' => '<h4 class="modal-title">创建</h4>',
    'footer' => '<a href="#" class="btn btn-primary" data-dismiss="modal">Close</a>',
]);
Modal::end();

Modal::begin([
    'id' => 'cancel-order-modal',
    'class' => 'modal modal-primary',
    'header' => '<h4 class="modal-title">订单作废</h4>',
    'footer' => '<a href="#" class="btn btn-primary" data-dismiss="modal">Close</a>',
    'options' => [
        'tabindex' => false
    ],
]);
Modal::end();



//观看房数的具体内容
$js = <<<JS
jQuery('.activity-cancel-sum-link').on('click',function() {
    $('.document-cancel-sum-form').remove();
    $.get(
        '/prod/prodinst/prodname',
        {
            id:$(this).data('order-id')
        },
        function (data) {
            $('#cancel-sum-modal').find('.modal-body').html(data);

        }
    );
});
JS;
$this->registerJs($js);

Modal::begin([
    'id' => 'cancel-sum-modal',
    'class' => 'modal modal-primary',
    'header' => '<h4 class="modal-title">带看的房源</h4>',
    'footer' => '<a href="#" class="btn btn-primary" data-dismiss="modal">Close</a>',

]);
Modal::end();

?>



